package com.example.widget_lib.widget;

import com.example.widget_lib.ResourceTable;
import com.example.widget_lib.utils.FormatUtil;
import com.example.widget_lib.utils.PxUtil;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;
import ohos.agp.utils.RectFloat;
import ohos.agp.utils.TextAlignment;
import ohos.app.Context;
import ohos.global.resource.Resource;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;

/**
 * 油表控件
 */
public class HalfCircleProView extends Component implements Component.DrawTask {
    /**
     * 圆心x坐标
     */
    private float centerX;
    /**
     * 圆心y坐标
     */
    private float centerY;

    /**
     * 扇形所在矩形
     */
    private RectFloat rectF = new RectFloat();

    /**
     * 进度动画
     */
    private AnimatorValue progressAnimator;

    /**
     * 动画执行时间
     */
    private int duration = 500;
    /**
     * 动画延时启动时间
     */
    private int startDelay = 500;

    /**
     * 圆形的画笔
     *
     * @param context
     */
    private Paint mProgressPaint;

    /**
     * 百分比的画笔
     */
    private Paint mTxtPaint;
    /**
     * 圆形的宽度
     *
     * @param context
     */
    private int mPaintWidth;

    /**
     * @param context
     */
    private PixelMap mBitmap;

    /**
     * 进度条的半径
     */
    private int mRadius;
    /**
     * 内环的半径
     */
    private int mInRadius;
    private Color[] mGradientColors = {new Color(0xffffbf05), new Color(0xFFFF6953)};
    private float mProgress;
    private float mStartProgress;
    private float mTempProgress;

    /**
     * 百分比文本的大小
     */
    private int mTxtSize;
    /**
     * 百分比的颜色
     */
    private Color mTxtColor;

    public HalfCircleProView(Context context, AttrSet attrSet) {
        super(context, attrSet);
        init();
        addDrawTask(this);
    }

    @Override
    public void addDrawTask(DrawTask task) {
        super.addDrawTask(task);
        task.onDraw(this, mCanvasForTaskOverContent);
    }

    private void init() {
        getAttr();
        mProgressPaint = getPaint(mPaintWidth, Color.RED);
        initTextPaint();
        initBitmap();
    }

    private Paint getPaint(int strokeWidth, Color color) {
        Paint paint = new Paint();
        paint.setStrokeWidth(strokeWidth);
        paint.setColor(color);
        paint.setAntiAlias(true);
        paint.setStrokeCap(Paint.StrokeCap.ROUND_CAP);
        paint.setStyle(Paint.Style.STROKE_STYLE);
        return paint;
    }

    private void initTextPaint() {
        mTxtPaint = new Paint();
        mTxtPaint.setTextSize(mTxtSize);
        mTxtPaint.setColor(mTxtColor);
        mTxtPaint.setTextAlign(TextAlignment.CENTER);
        mTxtPaint.setAntiAlias(true);
    }

    public void setProgress(float progress) {
        this.mProgress = progress;
        this.mStartProgress = progress;
        initAnimation();
    }

    public void setGradientColors(Color[] colors) {
        this.mGradientColors = colors;
    }

    private void getAttr() {
        mPaintWidth = (int) PxUtil.vp2px(2);
        try {
            int color = getResourceManager().getElement(ResourceTable.Color_color_2A2A2A).getColor();
            mTxtColor = new Color(color);
        } catch (Exception e) {
            e.printStackTrace();
        }
        mTxtSize = (int) PxUtil.vp2px(12);
    }

    private void initBitmap() {
        try {
            Resource resource = getResourceManager().getResource(ResourceTable.Media_icon_half_cicle);
            mBitmap = ImageSource.create(resource, new ImageSource.SourceOptions()).createPixelmap(null);
            mInRadius = mBitmap.getImageInfo().size.width / 2;
            mRadius = mInRadius + mPaintWidth;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void onSizeChanged() {
        centerX = getWidth() >> 1;
        centerY = getHeight() >> 1;
        rectF.left = centerX - mRadius;
        rectF.top = centerY - mRadius;
        rectF.right = centerX + mRadius;
        rectF.bottom = centerY + mRadius;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        onSizeChanged();
        Point[] points = new Point[]{new Point(centerX - mRadius, centerY), new Point(centerX + mRadius, centerY)};
        LinearShader shader = new LinearShader(points, null, mGradientColors, Shader.TileMode.MIRROR_TILEMODE);
        mProgressPaint.setShader(shader, Paint.ShaderType.LINEAR_SHADER);
        PixelMapHolder holder = new PixelMapHolder(mBitmap);
        float left = centerX - mInRadius;
        float right = left + mBitmap.getImageInfo().size.width;
        float top = centerY - mInRadius;
        float bottom = top + mBitmap.getImageInfo().size.height;
        RectFloat rectf = new RectFloat(left, top, right, bottom);
        canvas.drawPixelMapHolderRect(holder, rectf, mProgressPaint);
        canvas.drawText(mTxtPaint, FormatUtil.formatNumToPercentByTwoPoint(mProgress), centerX, centerY);
        mTempProgress = mProgress == 0 ? 0.1f : mProgress;
        float sweepAngele = mTempProgress * 180 / 100;
        canvas.drawArc(rectF, new Arc(180, sweepAngele, false), mProgressPaint);
    }

    private void initAnimation() {
        progressAnimator = new AnimatorValue();
        progressAnimator.setDuration(duration);
        progressAnimator.setDelay(startDelay);
        progressAnimator.setCurveType(Animator.CurveType.LINEAR);
        progressAnimator.setValueUpdateListener((animatorValue, v) -> {
            mProgress = Math.min(v * 180, mStartProgress);
            invalidate();
        });
        progressAnimator.start();
    }

}
